package com.dehui.traders.ui.fragment;

import android.databinding.ObservableArrayList;
import android.databinding.ObservableList;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.MenuItem;
import android.view.View;

import com.dehui.traders.BR;
import com.dehui.traders.R;
import com.dehui.traders.adapter.OptionalStockManagerAdapter;
import com.dehui.traders.bean.OptionalStockBean;
import com.dehui.traders.databinding.FragmentOptionalStockManagerBinding;
import com.dehui.traders.ui.viewmodle.OptionalStockManagerViewModel;
import com.yanzhenjie.recyclerview.swipe.touch.OnItemMoveListener;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import me.goldze.mvvmhabit.base.BaseFragment;

/**
 * Created by hqq on 18.4.19.
 */

public class OptionalStockManagerFragment extends BaseFragment<FragmentOptionalStockManagerBinding,OptionalStockManagerViewModel> implements OnItemMoveListener {

    public List<OptionalStockBean> stockList = new ArrayList<>();
    private OptionalStockManagerAdapter adapter;

    @Override
    public int initContentView() {
        return R.layout.fragment_optional_stock_manager;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public OptionalStockManagerViewModel initViewModel() {
        return new OptionalStockManagerViewModel(getContext());
    }

    @Override
    public void initData() {
        stockList.add(new OptionalStockBean("兴森科技","002345",null));
        stockList.add(new OptionalStockBean("华锋股份","002806",null));

        binding.recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        binding.recyclerView.setLongPressDragEnabled(true);
        binding.recyclerView.setOnItemMoveListener(this);

        adapter = new OptionalStockManagerAdapter(stockList, R.layout.list_item_optional_stock_manager,binding.recyclerView);
        binding.recyclerView.setAdapter(adapter);
    }


    @Override
    public boolean onItemMove(RecyclerView.ViewHolder srcHolder, RecyclerView.ViewHolder targetHolder) {
        if (srcHolder.getItemViewType() != targetHolder.getItemViewType()) return false;
        int fromPosition = srcHolder.getAdapterPosition();
        int toPosition = targetHolder.getAdapterPosition();

        Collections.swap(stockList, fromPosition, toPosition);
        adapter.notifyItemMoved(fromPosition, toPosition);
        return true;
    }

    @Override
    public void onItemDismiss(RecyclerView.ViewHolder srcHolder) {

    }


}
